package com.example.aritcleservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.aritcleservice.entity.AritleCategory;
import com.example.aritcleservice.entity.Articleinfo;
import com.example.aritcleservice.entity.VO.ArticleVO;
import com.example.aritcleservice.service.IAritleCategoryService;
import com.example.aritcleservice.service.IArticleinfoService;
import com.example.common.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 晨哥
 * @since 2024-05-01
 */
@RestController
@RequestMapping("/aritcle")
public class ArticleinfoController {
    @Autowired
    private IAritleCategoryService iAritleCategoryService;

    @Autowired
    private IArticleinfoService iArticleinfoService;

    // 添加文章
    @RequestMapping("/add")
    public AjaxResult addAritcleinfo(ArticleVO articleVO,
                                     @RequestHeader(value = "uid", required = false) Long uid) {  // 使用 vo 对象进行接收  false 不强制报错
        // 进行参数的校验
        if (articleVO == null || !StringUtils.hasLength(articleVO.getTitle()) ||
                !StringUtils.hasLength(articleVO.getContent())
                || articleVO.getCid() <= 0 || uid <= 0) {
            return AjaxResult.fail(-1, "非法参数!");
        }
        articleVO.setUid(uid);
        // 文章信息持久化
        boolean result = iArticleinfoService.save(articleVO);   // 这里会将文章 id 设置进来
        // 判断是否添加成功
        if (result) {
            // 此时创建文章跟标签的关联表
            AritleCategory aritleCategory = new AritleCategory();
            aritleCategory.setAid(articleVO.getAid());
            aritleCategory.setCid(articleVO.getCid());
            boolean finalResult = iAritleCategoryService.save(aritleCategory);
            return AjaxResult.success(finalResult);
        }

        return AjaxResult.fail(-2, "未知错误!");
    }

    @RequestMapping("/getbyId")
    public AjaxResult getArtById(Integer aid) {
        // 验证参数
        if (aid == null || aid <= 0) {
            return AjaxResult.fail(-1, "非法参数！");
        }
        // 进行文章的查询
        Articleinfo articleinfo = iArticleinfoService.getById(aid);
        if (articleinfo == null || articleinfo.getAid() <= 0) {
            return AjaxResult.fail(-2, "数据库暂无此文章信息！");
        }
        // 构建文章 VO 对象返回
        ArticleVO articleVO = new ArticleVO();
        articleVO.setTitle(articleinfo.getTitle());
        articleVO.setAid(articleinfo.getAid());
        articleVO.setContent(articleinfo.getContent());
        articleVO.setUid(articleinfo.getUid());
        // 通过文章的 aid 去文章关联表中查询 cid
        QueryWrapper<AritleCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aid", aid);
        AritleCategory aritleCategory = iAritleCategoryService.getOne(queryWrapper);
        if (aritleCategory != null) {
            // 去填充 cid
            articleVO.setCid(aritleCategory.getCid());
        }
        return AjaxResult.success(articleVO);
    }

    @RequestMapping("/update")
    public AjaxResult updateArtInfo(ArticleVO articleVO,
                                    @RequestHeader(value = "uid", required = false) Long uid) {
        // 进行参数的校验
        if (articleVO == null || !StringUtils.hasLength(articleVO.getTitle()) ||
                !StringUtils.hasLength(articleVO.getContent())
                || articleVO.getCid() <= 0 || uid <= 0 ||
                articleVO.getAid() == null || articleVO.getAid() <= 0) {
            return AjaxResult.fail(-1, "非法参数!");
        }
        // 对文章进行更新操作
        UpdateWrapper<Articleinfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("uid", uid);
        updateWrapper.eq("aid", articleVO.getAid());
        boolean result = iArticleinfoService.update(articleVO, updateWrapper);
        if (result) {
            // 此时更新成功
            QueryWrapper<AritleCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("aid", articleVO.getAid());
            AritleCategory aritleCategory = iAritleCategoryService.getOne(queryWrapper);
            if (aritleCategory != null && aritleCategory.getAcid() > 0) {
                // 先删除原来的关联表
                iAritleCategoryService.removeById(aritleCategory.getAcid());
            }
            // 重新添加关联表
            aritleCategory.setAid(articleVO.getAid());
            aritleCategory.setCid(articleVO.getCid());
            boolean finalResult = iAritleCategoryService.save(aritleCategory);
            return AjaxResult.success(finalResult);
        }

        return AjaxResult.fail(-2, "未知错误!");
    }
}
